package com.liu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.entity.Doctor;
import com.liu.entity.Register;
import com.liu.service.DoctorService;
import com.liu.service.RegisterService;
import com.liu.vo.DataView;
import com.liu.vo.RegisterVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @Autowired
    private DoctorService doctorService;

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "admin/register";
    }

    @RequestMapping("/listRegister")
    @ResponseBody
    public DataView listRegister(RegisterVo registerVo){
        IPage<Register> page = new Page<>(registerVo.getPage(), registerVo.getLimit());
        QueryWrapper<Register> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(registerVo.getId()),"rid",registerVo.getId());
        queryWrapper.like(StringUtils.isNotBlank(registerVo.getName()),"name",registerVo.getName());
        queryWrapper.like(registerVo.getDepartment() != null,"department",registerVo.getDepartment());
        registerService.page(page,queryWrapper);

        for(Register register : page.getRecords()){
            //为医生名字赋值
            if(register.getDid() != null){
                Doctor doctor = doctorService.getById(register.getDid());
                register.setDoctorName(doctor.getName());
            }
        }
        DataView dataView = new DataView(page.getTotal(),page.getRecords());
        return dataView;
    }

    /**
     * 初始化下列列表医生的数据
     */
    @RequestMapping("/listAllDoctor")
    @ResponseBody
    public List<Doctor> listAllDoctor(){
        List list = doctorService.list();
        return list;
    }

    /**
     * 增加挂号
     */
    @RequestMapping("/addRegister")
    @ResponseBody
    public DataView addRegister(Register register){
        DataView dataView = new DataView();
        boolean b = registerService.saveOrUpdate(register);
        if(b){
            dataView.setCode(200);
            dataView.setMsg("病人增加或者更改成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("病人增加或者更改失败");
        return dataView;
    }

    /**
     * 删除挂号
     */
    @RequestMapping("/deleteRegister")
    @ResponseBody
    public DataView deleteDoctor(Integer id){
        DataView dataView = new DataView();
        boolean b = registerService.removeById(id);
        if(b){
            dataView.setCode(200);
            dataView.setMsg("病人挂号删除成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("病人挂号删除失败");

        return dataView;
    }
}
